# PHY Test Makefile
# 支持多平台编译的网卡测试程序

# 默认编译器和工具链
CC = gcc
STRIP = strip

# 检查是否指定了特定的工具链
ifeq ($(ARCH), arm)
    CC = arm-nextvpu-linux-gnueabihf-gcc
    STRIP = arm-nextvpu-linux-gnueabihf-strip
    CFLAGS += -DARCH_ARM
endif

ifeq ($(ARCH), riscv)
    CC = riscv64-anyka-linux-musl-gcc
    STRIP = riscv64-anyka-linux-musl-strip
    CFLAGS += -DARCH_RISCV
endif

# 程序名称和版本
TARGET = phy_test
VERSION = 1.0.0

# 源文件和目标文件
SOURCES = main.c phy_utils.c
OBJECTS = $(SOURCES:.c=.o)
HEADERS = phy_utils.h

# 编译选项
CFLAGS += -Wall -Wextra -std=c99 -D_GNU_SOURCE
CFLAGS += -DVERSION=\"$(VERSION)\"

# 库链接选项
LDFLAGS = -pthread

# 调试和发布模式
ifeq ($(DEBUG), 1)
    CFLAGS += -g -O0 -DDEBUG
    BUILD_DIR = build/debug
else
    CFLAGS += -O2 -DNDEBUG
    BUILD_DIR = build/release
endif

# 目标目录中的对象文件
BUILD_OBJECTS = $(addprefix $(BUILD_DIR)/, $(OBJECTS))

# 默认目标
all: $(BUILD_DIR)/$(TARGET)

# 主程序编译
$(BUILD_DIR)/$(TARGET): $(BUILD_OBJECTS) | $(BUILD_DIR)
	@echo "Linking $@..."
	$(CC) $(BUILD_OBJECTS) $(LDFLAGS) -o $@
	@echo "Build complete: $@"

# 编译对象文件
$(BUILD_DIR)/%.o: %.c $(HEADERS) | $(BUILD_DIR)
	@echo "Compiling $<..."
	$(CC) $(CFLAGS) -c $< -o $@

# 创建构建目录
$(BUILD_DIR):
	@mkdir -p $(BUILD_DIR)

# 安装目标
install: $(BUILD_DIR)/$(TARGET)
	@echo "Installing $(TARGET)..."
	sudo cp $(BUILD_DIR)/$(TARGET) /usr/local/bin/
	sudo chmod +x /usr/local/bin/$(TARGET)
	@echo "Installation complete"

# 卸载目标
uninstall:
	@echo "Uninstalling $(TARGET)..."
	sudo rm -f /usr/local/bin/$(TARGET)
	@echo "Uninstall complete"

# 清理目标
clean:
	@echo "Cleaning build files..."
	rm -rf build/
	rm -f *.o $(TARGET)
	@echo "Clean complete"

# 发布版本编译
release: clean
	@$(MAKE) DEBUG=0

# 调试版本编译
debug: clean
	@$(MAKE) DEBUG=1

# ARM平台编译
arm: clean
	@$(MAKE) ARCH=arm

# RISC-V平台编译
riscv: clean
	@$(MAKE) ARCH=riscv

# 交叉编译所有平台
cross-compile: clean
	@echo "Building for x86_64..."
	@$(MAKE) release
	@mv $(BUILD_DIR)/$(TARGET) $(BUILD_DIR)/$(TARGET)-x86_64
	@echo "Building for ARM..."
	@$(MAKE) arm
	@mv $(BUILD_DIR)/$(TARGET) $(BUILD_DIR)/$(TARGET)-arm
	@echo "Building for RISC-V..."
	@$(MAKE) riscv
	@mv $(BUILD_DIR)/$(TARGET) $(BUILD_DIR)/$(TARGET)-riscv
	@echo "Cross-compilation complete"

# 运行测试
test: $(BUILD_DIR)/$(TARGET)
	@echo "Running basic tests..."
	@echo "Available network interfaces:"
	@ls /sys/class/net/ || true
	@echo "Testing help output:"
	@$(BUILD_DIR)/$(TARGET) --help || true

# 代码格式化
format:
	@which indent > /dev/null 2>&1 && \
	echo "Formatting source code..." && \
	indent -linux -i4 -ts4 *.c *.h && \
	echo "Format complete" || \
	echo "indent not found, skipping format"

# 代码静态分析
analyze:
	@which cppcheck > /dev/null 2>&1 && \
	echo "Running static analysis..." && \
	cppcheck --enable=all --std=c99 *.c *.h && \
	echo "Analysis complete" || \
	echo "cppcheck not found, skipping analysis"

# 创建发布包
package: cross-compile
	@echo "Creating release package..."
	@mkdir -p package/$(TARGET)-$(VERSION)
	@cp $(BUILD_DIR)/$(TARGET)-* package/$(TARGET)-$(VERSION)/
	@cp README.md package/$(TARGET)-$(VERSION)/
	@cp Makefile package/$(TARGET)-$(VERSION)/
	@cp *.c *.h package/$(TARGET)-$(VERSION)/
	@tar -czf $(TARGET)-$(VERSION).tar.gz -C package $(TARGET)-$(VERSION)
	@echo "Package created: $(TARGET)-$(VERSION).tar.gz"

# 显示帮助信息
help:
	@echo "PHY Test Build System"
	@echo "===================="
	@echo ""
	@echo "Available targets:"
	@echo "  all          - Build default target (release mode)"
	@echo "  release      - Build optimized release version"
	@echo "  debug        - Build debug version with symbols"
	@echo "  arm          - Cross-compile for ARM platform"
	@echo "  riscv        - Cross-compile for RISC-V platform"
	@echo "  cross-compile- Build for all supported platforms"
	@echo "  install      - Install binary to system"
	@echo "  uninstall    - Remove binary from system"
	@echo "  clean        - Remove build files"
	@echo "  test         - Run basic functionality tests"
	@echo "  format       - Format source code (requires indent)"
	@echo "  analyze      - Run static code analysis (requires cppcheck)"
	@echo "  package      - Create release package"
	@echo "  help         - Show this help message"
	@echo ""
	@echo "Environment variables:"
	@echo "  CC           - C compiler (default: gcc)"
	@echo "  CFLAGS       - Additional compiler flags"
	@echo "  LDFLAGS      - Additional linker flags"
	@echo "  DEBUG=1      - Build debug version"
	@echo "  ARCH=arm     - Build for ARM platform"
	@echo "  ARCH=riscv   - Build for RISC-V platform"

# 声明伪目标
.PHONY: all clean install uninstall release debug arm riscv cross-compile test format analyze package help

# 确保在并行构建时不会出现问题
.NOTPARALLEL: